$(function () {
    obj.init();
});
var obj = new function () {
    return {
        form: null,
        city: null,
        district: null,
        init: function () {
            this.form = new xhForm($('#xh_form'));
            this.form.parse().bindRequired($('#bt_modal_confirm',parent.document));
            this.funObj().addSelectData();//获取select数据
            this.listener();
        },
        listener: function () {
            var $this = this;
            $('#bt_modal_confirm',parent.document).click(function () {
                $this.funObj().saveData();
            });
            //省份更改
            $('#iProvince').change(function() {
                $this.funObj().changeProv($(this).val());
            }).trigger('change');
            //市更改
            $('#iCity').change(function() {
                $this.funObj().changeCity($(this).val());
            }).trigger('change');
            //运营区更改
            $('#iSecArea').change(function() {
                $this.funObj().changeSecArea($(this).val());
            }).trigger('change');
        },
        funObj: function () {
            var $this = this;
            return {
                addSelectData: function () {
                    $('#iType').select2({placeholder: '请选择', allowClear: true});
                    $('#iProvince').select2({placeholder: '请选择', allowClear: true});
                    $('#iSecArea').select2({placeholder: '请选择', allowClear: true});
                    $('#iCity').select2({placeholder: '请选择', allowClear: true});
                    $('#iCounty').select2({placeholder: '请选择', allowClear: true});
                    $('#iThirdArea').select2({placeholder: '请选择', allowClear: true});
                },
                changeProv: function (provId) {
                    $('#iCity').html('');
                    $('#iCounty').html('');
                    if (provId) {
                        xhExtra.ajaxRequest('customer/customer/getAreaById/' + provId, {}, {async:false}, function (result) {
                            var _param = [{id:'', text:'请选择'}];
                            if (result.length !== 0) {
                                for(var i = 0; i < result.length; i++){
                                    if (result[i].areaCode) {
                                        _param.push({id: result[i].areaCode, text: result[i].areaName});
                                    }
                                }
                            }
                            $('#iCity').select2({placeholder: '请选择', data: _param, allowClear: true});
                            $('#iCounty').select2({placeholder: '请选择', data: {}, allowClear: true});
                            // if ($this.city) {
                            //     cityPro.val([$this.city]).trigger('change');
                            // }
                        });
                    }
                },
                changeCity: function (cityId) {
                    $('#iCounty').html('');
                    if (cityId) {
                        xhExtra.ajaxRequest('customer/customer/getAreaById/' + cityId, {}, {async:false}, function (result) {
                            var _param = [{id:'', text:'请选择'}];
                            if (result.length !== 0) {
                                for(var i = 0; i < result.length; i++){
                                    if (result[i].areaCode) {
                                        _param.push({id: result[i].areaCode, text: result[i].areaName});
                                    }
                                }
                            }
                            $('#iCounty').select2({placeholder: '请选择', data: _param, allowClear: true});
                        });
                    }
                },
                changeSecArea: function (secAreaId) {
                    $('#iThirdArea').html('');
                    if (secAreaId) {
                        xhExtra.ajaxRequest('lm/housing/findArea/' + secAreaId, {}, {async:false}, function (result) {
                            $('#iThirdArea').select2({placeholder: '请选择', data: result, allowClear: true});
                        });
                    }
                },
                saveData:function () {
                    var params = {};
                    params.sProvince = $("#iProvince").find("option:selected").text();
                    params.sCity = $("#iCity").find("option:selected").text();
                    params.sCounty = $("#iCounty").find("option:selected").text();
                    $this.form.submit('lm/housing/saveInfo', params, function (res) {
                        if(res.code === 200){
                            window.top.xhTop.closeModal();
                            window.top.xhTop.getActiveFrame().obj.dataGrid.reload();
                        } else {
                            window.top.xhTop.showError(res.error);
                            xhExtra.setBtnNormal($('#bt_modal_confirm',parent.document));
                        }
                    },{check:false,success:true});
                }
            }
        }
    }
};